﻿var Note = {
    version: '1.0.0.0.0',
    ListNote: [],
    System: 1,
    Personal: 2,
    UserName: '',


    Init : function (userName) {
        $('form').jqTransform();
        Note.UserName = userName;    
        Note.GetByStatus(Note.System);
    },

    GetByStatus : function (status) {
        $('#listNote tbody').html('');
        var url = String.format('/GetRequest.ashx?m=note&action=getlistbystatus&status={0}&username={1}&rand={2}', status, Note.UserName, Math.random());
        $.getJSON(url, function (res) {
            if (res.Success) {
                if (res != null) {
                    if (status == Note.System) {
                        $.Mustache.load('/templates/note/system-list.htm')
                            .done(function () {
                                $('#listNote tbody').mustache('tpl-note-system-list', res);
                            });
                    }
                    else if (status == Note.Personal) {
                        $.Mustache.load('/templates/note/personal-list.htm')
                            .done(function () {
                                $('#listNote tbody').mustache('tpl-note-personal-list', res);
                            });
                    }
                }
            }
        });
    },

    SelectedType : function () {
        $.each($('#listNote #ddlStatus ul li a'), function () {
            if ($(this).attr('class') == 'selected') {
                var index = $(this).attr('index');
                var value = $('#listNote #ddlStatus select.jqTransformHidden option:eq(' + index + ')').attr('value');
                Note.GetByStatus(value);
            }
        });
    },

    Save : function () {
        var title   = $('#listNote #txtTitle').val();
        var content = $('#listNote #txtContent').val();    
        var status = 0;

        $.each($('#listNote #ddlStatus ul li a'), function () {
            if ($(this).attr('class') == 'selected') {            
                var index = $(this).attr('index');
                var value = $('#ddlStatus select.jqTransformHidden option:eq(' + index + ')').attr('value');
                status = value;
            }
        });

        if (status > 0 && Note.UserName != '') {
            var url = String.format('/GetRequest.ashx?m=note&action=save&title={0}&content={1}&status={2}&username={3}', title, content, status, Note.UserName);
            $.post(url, function () {
                $.jGrowl('Tạo mới ghi nhớ thành công!', { header: 'Thông báo:' });
                Note.GetByStatus(status);
            });
        }
    },

    Delete : function (id) {
        $('#listNote table tr').removeClass('gradeC');
        $('.n-' + id).addClass('gradeC');

        jConfirm('Bạn có chắc chắn muốn xóa?', 'Thông báo', function (result) {
            if (result) {
                var url = String.format('/GetRequest.ashx?m=note&action=delete&nId={0}', id);
                $.post(url, function () { })
                    .done(function () {
                        $.jGrowl(String.format('Xóa ghi nhớ thành công!'), { header: 'Thông báo:' });

                        var status = 0;
                        $.each($('#listNote #ddlStatus ul li a'), function () {
                            if ($(this).attr('class') == 'selected') {
                                var index = $(this).attr('index');
                                var value = $('#listNote #ddlStatus select.jqTransformHidden option:eq(' + index + ')').attr('value');
                                status = value;
                            }
                        });

                        Note.GetByStatus(status);
                    });
            }
        });
    }

};